#pragma once


#include<vector>

namespace bit
{
	template < class T >
	class Less
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x < y;
		}

	};

	template < class T >
	class Greater
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x > y;
		}

	};

	template<class T, class Container = vector<T>, class Compare = Less<T >>
	class priority
	{
	public:


		void AdjustUp(int child)
		{
			Compare  com;
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				if (com(_con[parent], _con[child]))
				{
					swap(_con[parent], _con[child]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}

		}


		/*	void AdjustUp(int parent)
			{
				int child = parent * 2 + 1;
				while (child < _con.size())
				{
					if ( child +1 < _con.size() && con[child + 1] < con[child])
					{
						++child;
					}

					if (con[child] < con[parent])
					{
						swap(con[child], con[parent]);
						parent = child;
						child = parent * 2 + 1;
					}
					else
					{
						break;
					}
				 }

			}*/
		void AdjustDown(int parent, class Compare = Less<T >)
		{
			Compare  com;
			int child = parent * 2 + 1;
			while (_con[child] < _con.size())
			{
				if (com(_con[child] < _con[child + 1]) && _con[child + 1] < _con.size())
				{
					++child;
				}

				if (com(_con[parent] > _con[child]))
				{
					swap(_con[parent], _con[child]);
					parent = child;
					child = parent * 2 + 1;
				}

				else {
					break;
				}
			}

		}
		void pop()
		{
			swap(_con[0], _con[con.size() - 1]);
			_con.pop_back();
			AdjustDown(0);
		}
		void push(const  T& x)
		{
			_con.push_back(x);
			AdjustUp(_con.size() - 1);
		}

		const T& top()
		{
			return _con[0];
		}

		size_t size() const
		{
			return _con.size();
		}
		bool empty()const
		{
			return _con.empty();
		}

	private:
		Container _con;
	};
}